<?php
namespace App\Model;



use Common\Util\ConfigUtils;

class ItemsModel extends BaseModel {

    private static $instance;

    public static function singleton()
    {
        self::$instance === null and self::$instance = new self();
        return self::$instance;
    }

    //获取主题推荐单品
    public function getTopicItem($topic_code,$city_code){
        $where['i.is_show']=1;
        $where['i.index_recommend']=1;
        $where['i.topic_field']=$topic_code;
        $where['ic.city_code']=$city_code;
        $rows =$this->alias('i')
            ->join('__ITEMS_CITY__ ic on i.id=ic.item_id')
            ->where($where)
            ->field('i.item_name,i.psp,i.unit,i.item_key,i.height,i.width,i.img_name')
            ->order('sort desc')
            ->select();
        foreach ($rows as $key=>$val){
            $rows[$key]['item_key']=ConfigUtils::getConfig('SERVER_URL')."/Public/item_img/".$val['img_name'].'.jpg';
        }
        return $rows;
    }


    //获取单品列表
    public function getList($topic_code,$city_code,$page_no,$page_size){

        $where['i.is_show']=1;
        //$where['i.index_recommend']=1;
        $where['i.topic_field']=$topic_code;
        $where['ic.city_code']=$city_code;
        $rows =$this->alias('i')
            ->join('__ITEMS_CITY__ ic on i.id=ic.item_id')
            ->where($where)
            ->field('i.id,i.item_name,i.psp,i.unit,i.item_key,i.height,i.width,i.img_name')
            ->limit($page_no*$page_size-$page_size,$page_size)
            ->order('sort desc,id asc')
            ->select();
      
        foreach ($rows as $key=>$val){
            $rows[$key]['item_key']=ConfigUtils::getConfig('SERVER_URL')."/Public/item_img/".$val['img_name'].'.jpg';
        }
        return $rows;
    }

    //深圳、广州单品列表

    public function getItems($city_codes,$page_no,$page_size){

        $where['i.is_show']=1;
        //$where['i.index_recommend']=1;
        $where['ic.city_code']=array('IN',$city_codes);

        $rows =$this->alias('i')
            ->join('__ITEMS_CITY__ ic on i.id=ic.item_id')
            ->where($where)
            //->where('topic_field is not null')
            ->field('i.id,i.item_name,i.psp,i.unit,i.item_key,i.topic_field,i.item_code')
            ->limit($page_no*$page_size-$page_size,$page_size)
            ->order('sort desc')
            ->group('i.id')
            ->select();

        foreach ($rows as $key=>$val){
            //$rows[$key]['item_key']=ConfigUtils::getConfig('SERVER_URL')."/Public/item_img/".$val['item_key'].'.jpg';
        }
        return $rows;
    }

}